// by Xeno
private ["_direction", "_numconfv", "_newgroup", "_reta", "_vehicles", "_next_pos", "_leader", "_i", "_wp", "_endtime"];


_pos_start = _this select 0;
_pos_end = _this select 1;
_direction = _this select 2;
_type_array = _this select 3;
_side = d_enemy_side;
if (count _this > 4) then { _side = _this select 4; };
diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"sideconvoy.sqf","Start",_this];
_crew_member = "";

d_confvdown = 0;
_numconfv = count _type_array;
_allSMVecs = [];
_newgroup = [_side] call x_creategroup;
_roads = _pos_start nearRoads 300;
_road = (_roads call XfRandomArrayVal);
_roads = _pos_start nearRoads 80;
_next_pos = (getPos (_roads select 0)) findEmptyPosition [0, 10];
_next_pos set [2,0];
_reta = [1,_next_pos, (_type_array select 0), _newgroup,0,_direction] call x_makevgroup;
if (count _roads <= 1) then {
	_roads = (_next_pos nearRoads 80) call xfarraypushstack;
};
// [true,[format["pos_%1",_k],_next_pos, "ELLIPSE", "ColorRed", [1,1],"",0,"","",0.7]] call XfMissionMarker;
diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"sideconvoy.sqf","Created",_reta,(_type_array select 0)];
// _reta = [1, _pos_start, (GVAR(sm_convoy_vehicles) select 0), _newgroup, 0, _direction] call FUNC(makevgroup);
_vehicles = _reta select 0;
(_vehicles select 0) setDir _direction;
// (_vehicles select 0) addEventHandler ["killed", {d_confvdown = d_confvdown + 1;}];
{
	_vec = _x;
	_vec lock true;
	_vec setDir _direction;
	_vec addEventHandler ["killed", {d_confvdown = d_confvdown + 1;}];
	[_vec,_newgroup,(getPos _vec),"REGISTER",[]] call XfMissionCreateMissionUnit;
	{
		[_x,_newgroup,(getPos _vec),"REGISTER",[]] call XfMissionCreateMissionUnit;
	} foreach crew _vec;
	_allSMVecs set [count _allSMVecs, _vec];
} foreach _vehicles;
_leader = leader _newgroup;
_leader setRank "LIEUTENANT";
_newgroup allowFleeing 0;
_newgroup setCombatMode "GREEN";
_newgroup setFormation "NONE";
_newgroup setSpeedMode "NORMAL";
sleep 1.933;
_vehicles = nil;
for "_k" from 1 to (_numconfv - 1) do {
	// _reta = [1, _next_pos, (GVAR(sm_convoy_vehicles) select _i), _newgroup, 20, _direction] call FUNC(makevgroup);
	_reta = [1,_next_pos, (_type_array select _k), _newgroup,0,_direction] call x_makevgroup;
	diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"sideconvoy.sqf","Created",_reta,(_type_array select _k)];
	_vehicles = _reta select 0;
	(_vehicles select 0) setDir _direction;
	_next_pos = (getPos (_roads select _k)) findEmptyPosition [0, 10];
	_next_pos set [2,0];
	if (count _roads <= (_k + 1)) then {
		_roads = (_next_pos nearRoads 80) call xfarraypushstack;
	};
	// [true,[format["pos_%1",_k],_next_pos, "ELLIPSE", "ColorRed", [1,1],"",0,"","",0.7]] call XfMissionMarker;
	{
		_vec = _x;
		_vec lock true;
		_vec setDir _direction;
		_vec addEventHandler ["killed", {d_confvdown = d_confvdown + 1;}];
		[_vec,_newgroup,(getPos _vec),"REGISTER",[]] call XfMissionCreateMissionUnit;
		{
			[_x,_newgroup,(getPos _vec),"REGISTER",[]] call XfMissionCreateMissionUnit;
		} foreach crew _vec;
		_allSMVecs set [count _allSMVecs, _vec];
	} foreach _vehicles;

	sleep 1.933;
	_vehicles = nil;
};

for [{_i = 0},{_i < count(waypoints _newgroup)},{_i = _i + 1}] do {
	deleteWaypoint [_newgroup, _i];
};

_newgroup setCombatMode "RED";
_newgroup setFormation "COLUMN";
_newgroup setSpeedMode "NORMAL";

_wp = _newgroup addWaypoint[_pos_end, 0];
_wp setWaypointBehaviour "SAFE";
_wp setWaypointSpeed "NORMAL";
_wp setwaypointtype "MOVE";
_wp setWaypointFormation "COLUMN";
_wp setWaypointCompletionRadius 200;
_wp setWaypointTimeout [10,15,25];
_wp setWaypointCompletionRadius 200;
_newgroup setCurrentWaypoint _wp;

sleep 20.123;

private ["_convoy_close_dest","_convoy_reached_dest","_convoy_destroyed"];
_convoy_close_dest = false;
_convoy_reached_dest = false;
_convoy_destroyed = false;
_endtime = time + 3600;

while {true} do {
	if (X_MP) then {
		if ((call XPlayersNumber) == 0) then {
			if (!d_all_simulation_stopped) then {
				d_all_simulation_stopped = true;
				{_x enableSimulation false} forEach allUnits;
			};
		};
		waitUntil {sleep (1.012 + random 1);(call XPlayersNumber) > 0};
		if (d_all_simulation_stopped) then {
			{_x enableSimulation true} forEach allUnits;
			d_all_simulation_stopped= false;
		};
	};
	if (d_confvdown == _numconfv) exitWith {
		_convoy_destroyed = true;
	};
	if ({canMove _x} count _allSMVecs == 0) exitWith {
		_convoy_destroyed = true;
	};
	_units = units _newgroup;
	if ((position (_units select ((count _units) - 1))) distance _pos_end < 300) exitWith {
		_convoy_close_dest = true;
	};
	if (time > _endtime) exitWith {_convoy_reached_dest = true;};
	{
		(driver _x) doMove _pos_end;
	} foreach _allSMVecs;
	sleep 5.123;
};
if (_convoy_close_dest) then {
	private ["_driver","_crew","_grp","_leader"];
	_units = units _newgroup;
	_driver = (vehicle (_units select ((count _units) - 1)));
	_crew = crew (vehicle _driver);
	_grp = [d_side_enemy] call x_creategroup;
	{[_x] joinSilent _grp;} forEach _crew;
	_wp = _grp addWaypoint[_pos_end,0];
	while {true} do {
		if (X_MP) then {
			if ((call XPlayersNumber) == 0) then {
				if (!d_all_simulation_stopped) then {
					d_all_simulation_stopped = true;
					{_x enableSimulation false} forEach allUnits;
				};
			};
			waitUntil {sleep (1.012 + random 1);(call XPlayersNumber) > 0};
			if (d_all_simulation_stopped) then {
				{_x enableSimulation true} forEach allUnits;
				d_all_simulation_stopped= false;
			};
		};
		if (d_confvdown == _numconfv) exitWith {_convoy_destroyed = true;};
		_leader = leader _grp;
		if (_leader distance _pos_end < 30) exitWith {_convoy_reached_dest = true;};
		if (time > _endtime) exitWith {_convoy_reached_dest = true;};
		{
			(driver _x) doMove _pos_end;
		} foreach _allSMVecs;
		sleep 5;
	};
	sleep 60;
};
_convoy_reached_dest;